package hustlas.ball.driver;

import hustlas.ball.preparse.GrammarGen;
import hustlas.ball.preparse.Lexer;
import hustlas.ball.preparse.PTable;
import hustlas.ball.preparse.Tokens;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.Stack;

/**
 * This class serves as the main method that draws all components of the parser
 * together. It handles input from the command line and drawing input from the
 * files that are inserted into it and ultimately writing output files
 * 
 * @author Steven
 *
 */

public class ParseRunner {
	//TODO: make this parse the files or w/e that need to be passed in
	public static void main(String[] args) {
	    ArrayList<Tokens> tokeUp = new ArrayList<Tokens>();
		String code = "";
	    if(args[1] == null){
	        System.err.println("Incorrect Input Error");
	    }
	    try {
            BufferedReader br = new BufferedReader(new FileReader(args[0]));
            String codeLine;
            while((codeLine = br.readLine()) != null){
                code += codeLine + "\n";
            }
            br.close();
        } catch (FileNotFoundException e) {
            System.out.println("Sorry, that file isn't found");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
	    String rules = "";
	    try{
	        BufferedReader hue = new BufferedReader(new FileReader(args[1]));
	        String rulesLine;
	        while((rulesLine = hue.readLine()) != null){
                rules += rulesLine + "\n";
            }
	        String[] splitRules = rules.split("%[a-zA-z] ");
	        splitRules[0].replace("\n", "");
	        String[] tokens = splitRules[0].split(" ");
	        splitRules[2].replace("\n", "");
	        splitRules[1].replace("\n", "");
	        EnumMap<Tokens, Boolean> eMap = new EnumMap<Tokens, Boolean>(Tokens.class);
	        
	        for(String token:tokens){
	            eMap.put(Tokens.getToken(token), true);
	            tokeUp.add(Tokens.getToken(token));
	        }
	        Lexer lexer = new Lexer(eMap);
	        Stack<Tokens> tokeList = lexer.tokenize(code);
	        GrammarGen grammar = new GrammarGen(splitRules[2], splitRules[3]);
	        grammar.removeLeftRecursion();
	        PTable table = new PTable(grammar);
	        table.makeFirstSet();
	        table.makeFollowSet();
	        ParserDriver driver = new ParserDriver(tokeList, splitRules[1].split(" "), splitRules[2]);
	        driver.parse(table.generateTable(tokeUp));
	        //TODO push this shit into parsedriver
	        //Code is located in code
	        //start state is located in splitRules[2]
	        //Rules is located in splitRules[3]
	        //non terminals is located in splitRules[1], NOTE: not split
	        
	    } catch (FileNotFoundException e) {
	        System.out.println("Sorry, that file isn't found");
	    } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
	    
	}
}
